'use strict';
const db = uniCloud.database();
const dbForJQL = uniCloud.databaseForJQL();
const businessDBName = 'zhy_business_info';
const userDBName = 'zhy_user_info';
const userConsumeDBName = 'zhy_consume_record';
const allowenceDBName = 'zhy_allowence_record';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)

	//返回数据给客户端
	return queryUserAndBusinessAllowenceRecord(event, context)
};
async function queryUserAndBusinessAllowenceRecord(event, context) {
	let result = [];
	if (event == null || event == '' || event == undefined || JSON.stringify(event) == '{}') {
		return {
			errSubject: 'param_lack',
			errCode: 0,
			errMsg: '参数不齐',
			data: []
		}
	}
	if (event.queryType == 1) {
		// 查询用户补贴记录
		return queryUserAllowenceRecord(event, context)
	} else if (event.queryType == 2) {
		// 查询商户的补贴记录
		return queryBusinessAllowenceRecord(event, context)
	}

}
async function queryUserAllowenceRecord(event, context) {
	let params = {
		user_id: event.user_id
	}
	// 'user_id,business_id,zhb_allowence_amount,allowence_time'
	const result = await db.collection(allowenceDBName).where(params).field({
			user_id: true,
			business_id: true,
			zhb_allowence_amount: true,
			allowence_time: true,
			// bs_reverse_allowence_amount: false,
		})
		.orderBy('allowence_time', 'desc').skip(event.skip).limit(event.size).get()
	console.log(JSON.stringify(result));
	return result.data
}
async function queryBusinessAllowenceRecord(event, context) {
	let params = {
		business_id: event.business_id
	}
	const result = await db.collection(allowenceDBName).where(params).field({
			user_id: true,
			business_id: true,
			bs_reverse_allowence_amount: true,
			allowence_time: true,
			// zhb_allowence_amount: false
		})
		.orderBy('allowence_time', 'desc').skip(event.skip).limit(event.size).get()
	return result.data
}